const http = require("http");

const json = '{ "a": "3", "b": "4" }';

// createServer用于启动静态服务
const server = http.createServer((request, response) => {
  // request 请求, 前端发过来的东西
  // response 相应, 返回给前端的东西

  console.log(request.url);

  // response.write("hello world");

  // 添加一个响应头
  response.writeHead(200, {
    "content-type": "application/json;charset=UTF-8",
  });
  // response.write("<h1>hello 世界</h1>");

  response.write(json);

  // 添加句柄函数，表示发完了
  response.end();
});

// 做端口号的监听
server.listen(3000, "127.0.0.1", () => {
  // 监听成功以后做的事情
  console.log("the server is running at http://127.0.0.1:3000");
});

// http的媒体类型
/*
  .htm .html	HyperText Markup Language (HTML)	  text/html
  .txt	Text, (generally ASCII or ISO 8859-n)	    text/plain
  .json	JSON format	                              application/json
  .png	Portable Network Graphics	                image/png
  .jpeg .jpg	JPEG images	                        image/jpeg
  .xls	Microsoft Excel	                          application/vnd.ms-excel
  .xlsx	Microsoft Excel (OpenXML)	           application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
  .pdf	Adobe Portable Document Format (PDF)	application/pdf
  .zip	ZIP archive	                          application/zip
*/
